/*
 * @file yt_my_top_bar
 * @Author: liuer
 * @Date: 2024/8/16 17:14
 * @LastModified: 2024-08-16 17:14
 * @LastModifiedBy: liuer
 */

import 'dart:async';

import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:yt_devops/components/app_permission/yt_tab_view_model.dart';
import 'package:yt_devops/components/event_bus_util/eventbus_util.dart';
import 'package:yt_devops/components/log_utils.dart';
import 'package:yt_devops/pages/site/site_observer_list/views/my_top_tabbar.dart';
import 'package:yt_devops/utils/func_utils.dart';

class YtMyTopBar extends StatefulWidget {
  final List<YtTabViewModel> bottomBarList;
  final void Function(int)? onTap;///需要把TabController传递出去，不然会报错
  const YtMyTopBar(this.bottomBarList,this.onTap,{super.key});


  @override
  State<YtMyTopBar> createState() => _YtMyTopBarState();
}

class _YtMyTopBarState extends State<YtMyTopBar> with TickerProviderStateMixin{
  int selectedIndex = 0;
  List<String> items = [];
  TabController? tabController;
  StreamSubscription? changeLanguageSubscription;
  @override
  void initState() {
    // TODO: implement initState
    _initItems();
    tabController = TabController(length: items.length, vsync: this);
    tabController?.addListener(() {
      if(tabController!.indexIsChanging){
         widget.onTap!(tabController!.index);
       }
    });
    changeLanguageSubscription =
        EventBusUtil.getInstance().on<SwitchLanguage>().listen((event) {
          Log.d('YtBottomNavigationBarSwitchLanguage===');
          _initItems(isFirstLoad: false);
        });
    super.initState();
  }
  _initItems({bool isFirstLoad = true}){
    items.clear();
    for (var element in widget.bottomBarList) {
      if(FuncUtils.isPassAuthority([element.permissionString]) && element.isShow) {
        items.add(element.labelString.tr);
      }
    }
    if(!isFirstLoad){
      setState(() {

      });
    }

  }
  @override
  Widget build(BuildContext context) {
    return MyTopTabBar(tabController!, items);
  }
}
